<template>
  <div class="uploadImage_group_box clearfix">
    <div class="uploadImage_group_items" v-for="(item, index) in list" :key="index">
      <UploadImage
        :imgName="item.name"
        v-model="item.value"
        @change="change"
      ></UploadImage>
    </div>
  </div>
</template>

<script>
import UploadImage from './index'
import Emitter from 'element-ui/src/mixins/emitter'
export default {
  name: 'uploadImageGroup',
  mixins: [Emitter],
  components: {
    UploadImage
  },
  props: {
    labelNames: {
      type: String,
      default: ''
    },
    value: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      list: []
    }
  },
  watch: {
    labelNames () {
      this.init()
    }
  },
  computed: {
    modelValue: {
      get () {
        return this.value
      },
      set (val) {
        this.$emit('input', val)
        this.dispatch('ElFormItem', 'el.form.change', val)
      }
    }
  },
  methods: {
    change () {
      let str = ''
      for (let i = 0; i < this.list.length; i++) {
        str += `${this.list[i].value},`
      }
      this.modelValue = str.substring(0, str.lastIndexOf(','))
    },
    init () {
      let list = []
      if (this.labelNames) {
        let nameList = this.labelNames.split(',')
        for (let i = 0; i < nameList.length; i++) {
          list.push({
            name: nameList[i],
            value: ''
          })
        }
      }
      this.list = list
    }
  },
  created () {},
  mounted () {
    this.init()
  }
}
</script>
<style lang="less" scoped>
.uploadImage_group_box{
  display: inline-block;
  .uploadImage_group_items
  {
    float: left;
    margin-right: 10px;
    margin-bottom: 10px;
  }
}
</style>
